package com.lsx.logisticscore.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lsx.logisticscore.entity.Resource;
import com.lsx.logisticscore.mapper.AccountMapper;
import com.lsx.logisticscore.mapper.AdminMapper;
import com.lsx.logisticscore.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.*;

/**
 * @Author: Liang
 * @Date: 2021/12/25
 */
@Service
public class AdminService {
    @Autowired
    private AdminMapper adminMapper;

    /**
     * 根据角色获取资源列表
     * @param role
     * @return 资源列表
     */
    public List<Resource> getResourceByUser(String role){

        QueryWrapper<Resource> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role", role)
                    .isNull("parent");

        List<Resource> resources = adminMapper.selectList(queryWrapper);
        for (Resource r: resources
             ) {
            r.setChildren(getResourceChildren(r.getResourceId()));
        }
        return resources;
    }




    private List<Resource> getResourceChildren(Integer parentId){
        List<Resource> children = adminMapper.selectChildren(parentId);
        for (Resource child: children
             ) {
            child.setChildren(getResourceChildren(child.getResourceId()));
        }
        return children;
    }
}
